package cn.elead.chaos.email.service.impl;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;

import javax.mail.MessagingException;

import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.util.CollectionUtils;

import cn.elead.chaos.email.domain.CustomMailMessage;
import cn.elead.chaos.email.service.ISubjectAttachService;
import cn.elead.chaos.email.template.TemplateProcess;

public class EmailProcessor {

	@Autowired
	private ISubjectAttachService subjectService;

	@Autowired
	private TemplateProcess templateProcess;

	public MimeMessageHelper createMimeMessageHelper(JavaMailSender mailSender) throws MessagingException {

		return new MimeMessageHelper(mailSender.createMimeMessage(), true, StandardCharsets.UTF_8.name());
	}

	public void subjectContext(CustomMailMessage message) {

		// 获取邮件主题和模板
		subjectService.loadSubjectContext(message);
		if (null == message) {
			throw new RuntimeException("没有获取到邮件主题和模板");
		}
		// 邮件内容
		message.setText(templateProcess.convertTemplate(message));
	}

	public void attachment(CustomMailMessage message, MimeMessageHelper helper) throws MessagingException {
		// 添加附件
		subjectService.loadAttachment(message);
		// 添加附件
		Map<String, File> attachments = message.getAttachments();
		if (!CollectionUtils.isEmpty(attachments)) {
			Set<Entry<String, File>> attachmentEntrySet = attachments.entrySet();
			for (Entry<String, File> attachment : attachmentEntrySet) {
				// 建议文件带上后缀，可支持在线预览
				helper.addAttachment(attachment.getKey(), attachment.getValue());
			}
		}
	}

	public void resource(CustomMailMessage message, MimeMessageHelper helper) throws MessagingException {
		// 添加正文中有静态资源
		subjectService.loadResource(message);
		// 添加正文中有静态资源
		Map<String, File> resources = message.getAttachments();
		if (!CollectionUtils.isEmpty(resources)) {
			Set<Entry<String, File>> resourcesEntrySet = resources.entrySet();
			for (Entry<String, File> resource : resourcesEntrySet) {
				// 建议文件带上后缀，可支持在线预览
				helper.addInline(resource.getKey(), resource.getValue());
			}
		}
	}

	/**
	 * 对象转换
	 * 
	 * @param message
	 * @param helper
	 * @throws MessagingException
	 */
	public void convert(CustomMailMessage message, MimeMessageHelper helper) throws MessagingException {
		helper.setFrom(message.getFrom());
		helper.setTo(message.getTo());
		if (null != message.getCc()) {
			helper.setCc(message.getCc());
		}
		if (null != message.getBcc()) {
			helper.setBcc(message.getBcc());
		}
		if (StringUtils.isNotBlank(message.getSubject())) {
			// 邮件主题
			helper.setSubject(message.getSubject());
		}
		if (StringUtils.isNotBlank(message.getText())) {
			// 邮件内容
			helper.setText(message.getText(), true);
		}

	}
}
